package com.neusoft.elmboot.service.impl;

import java.util.List;

import com.neusoft.elmboot.service.ex.DataNotFoundException;
import com.neusoft.elmboot.service.ex.DeleteException;
import com.neusoft.elmboot.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.mapper.DeliveryAddressMapper;
import com.neusoft.elmboot.po.DeliveryAddress;
import com.neusoft.elmboot.service.DeliveryAddressService;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService{
	
	@Autowired
	private DeliveryAddressMapper deliveryAddressMapper;
	
	@Override
	public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
		List<DeliveryAddress> list = deliveryAddressMapper.listDeliveryAddressByUserId(userId);
		if(list != null && !list.isEmpty()){
			return list;
		}
		else{
			throw new DataNotFoundException("该用户不存在或该用户收货地址列表为空，获取用户收货地址列表失败!");
		}
	}
	
	@Override
	public DeliveryAddress getDeliveryAddressById(Integer daId) {
		DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddressById(daId);
		if(deliveryAddress == null) {
			throw new DataNotFoundException("收货地址不存在，根据daId获取收货地址失败！");
		}
		return deliveryAddress;
	}
	
	@Override
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
		int result = deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
		if(result <= 0) {
			throw new InsertException("保存收货地址失败！");
		}
			return result;
	}
	
	@Override
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
		int result = deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
		if(result <= 0) {
			throw new InsertException("更新收货地址失败！");
		}
			return result;
	}
	
	@Override
	public int removeDeliveryAddress(Integer daId) {
		int result = deliveryAddressMapper.removeDeliveryAddress(daId);
		if(result <= 0) {
			throw new DeleteException("删除收货地址失败！");
		}
			return result;
	}
}
